package com.wind.test;

import com.wind.entity.Cache;
import org.junit.Test;

import javax.persistence.*;

public class JpaTest04_缓存 {

  /*
    插入数据
   */
  @Test
  public void insert() {
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("JpaDemo");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();

    Cache cache = new Cache();
    cache.setName("测试缓存~！");

    entityManager.persist(cache);

    transaction.commit();
    entityManager.close();
    entityManagerFactory.close();
  }

  /*
    测试一级缓存
   */
  @Test
  public void testLevel1Cache() {
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("JpaDemo");
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();

    Cache cache1 = entityManager.find(Cache.class, 1);
    System.out.println(cache1.getName());
    Cache cache2 = entityManager.find(Cache.class, 1);
    System.out.println(cache2.getName());
    // 运行结果：只有一条查询语句，就说明默认 开启了 一级缓存

    transaction.commit();
    entityManager.close();
    entityManagerFactory.close();
  }

  /*
    测试二级缓存：未开启的效果（没配置之前）
   */
  @Test
  public void testLevel2CacheNoOpen() {
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("JpaDemo");

    EntityManager entityManager1 = entityManagerFactory.createEntityManager();
    EntityTransaction transaction1 = entityManager1.getTransaction();
    transaction1.begin();
    Cache cache1 = entityManager1.find(Cache.class, 1);
    System.out.println(cache1.getName());
    transaction1.commit();
    entityManager1.close();

    // 在查询第二次前关闭 EntityManager 再开启
    EntityManager entityManager2 = entityManagerFactory.createEntityManager();
    EntityTransaction transaction2 = entityManager2.getTransaction();
    transaction2.begin();
    Cache cache2 = entityManager2.find(Cache.class, 1);
    System.out.println(cache2.getName());
    transaction2.commit();
    entityManager2.close();

    // 测试结果：两条查询语句。这说明两个 EntityManager 不互通~！

    entityManagerFactory.close();
  }

  /*
    测试二级缓存：开启效果
   */
  @Test
  public void testLevel2CacheOpen() {
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("JpaDemo");

    EntityManager entityManager1 = entityManagerFactory.createEntityManager();
    EntityTransaction transaction1 = entityManager1.getTransaction();
    transaction1.begin();
    Cache cache1 = entityManager1.find(Cache.class, 1);
    System.out.println(cache1.getName());
    transaction1.commit();
    entityManager1.close();

    // 在查询第二次前关闭 EntityManager 再开启
    EntityManager entityManager2 = entityManagerFactory.createEntityManager();
    EntityTransaction transaction2 = entityManager2.getTransaction();
    transaction2.begin();
    Cache cache2 = entityManager2.find(Cache.class, 1);
    System.out.println(cache2.getName());
    transaction2.commit();
    entityManager2.close();

    // 测试结果：只有一条查询语句。这说明两个 EntityManager 互通了，二级缓存生效~！

    entityManagerFactory.close();
  }
}
